package com.yuerlive.rainbow.module.recharge.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.common.BaseRecycleViewAdapter;
import com.yuerlive.rainbow.entity.RechargeInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by arvin on 2016/9/27.
 */
public class RechargeItemAdapter extends BaseRecycleViewAdapter {
    private List<RechargeInfo> mDatas;
    private int isFirst;

    private int selected = 0;

    public RechargeItemAdapter(Context mContext, List<RechargeInfo> mDatas, int isFirst) {
        super(mContext);
        this.mDatas = mDatas;
        this.isFirst = isFirst;
    }

    public void setIsFirst(int isFirst) {
        this.isFirst = isFirst;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.layout_recharge_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder itemHolder = (ViewHolder) holder;
        holder.itemView.setOnClickListener(v -> {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, position);
                selected = position;
            }
            holder.itemView.setBackgroundResource(R.drawable.recharge_selected);
        });

        if (position == selected){
            holder.itemView.setBackgroundResource(R.drawable.recharge_selected);
        }else {
            holder.itemView.setBackgroundResource(R.drawable.recharge_unselected);
        }

        if (isFirst == 0) {
            itemHolder.ivFirst.setVisibility(View.GONE);
        } else {
            itemHolder.ivFirst.setVisibility(View.VISIBLE);
        }
        itemHolder.tvCoins.setText(mDatas.get(position).getValue() + "彩虹糖");
        itemHolder.tvMoney.setText(mDatas.get(position).getRmb() + "元");
        itemHolder.tvExp.setVisibility(mDatas.get(position).getExpe()==0?View.GONE:View.VISIBLE);
        itemHolder.tvExp.setText("赠送" + mDatas.get(position).getExpe() + "经验值");

    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tvCoins)
        TextView tvCoins;
        @BindView(R.id.tvMoney)
        TextView tvMoney;
        @BindView(R.id.tvExp)
        TextView tvExp;
        @BindView(R.id.ivFirst)
        ImageView ivFirst;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
